package com.sctvcloud.dongqu.ui.activities;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.widget.CustomFontEditText;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.base.BaseActivity;
import com.sctvcloud.dongqu.beans.LiveAdvance;
import com.sctvcloud.dongqu.beans.LiveAdvanceInfo;
import com.sctvcloud.dongqu.http.AbsNetCallBack;
import com.sctvcloud.dongqu.http.NetUtils;
import com.sctvcloud.dongqu.http.ParamsEditor;
import com.sctvcloud.dongqu.ui.dialog.ChannelDiaFragment;
import com.sctvcloud.dongqu.ui.dialog.ConfirmDiaFragment;
import com.sctvcloud.dongqu.ui.fragment.TvFragment;
import com.sctvcloud.dongqu.ui.widget.BasePlayerView;
import com.sctvcloud.dongqu.utils.ShareUtils;
import com.sctvcloud.dongqu.utils.UserManager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class LiveConfigActivity extends BaseActivity {

    @BindView(R.id.title_top_logo_layout)
    protected LinearLayout title_top_logo_layout;
    @BindView(R.id.title_top_title)
    protected CustomFontTextView title_top_title;
    @BindView(R.id.title_top_back)
    protected ImageView title_top_back;

    @BindView(R.id.tv_start)
    protected TextView tv_start;

    @BindView(R.id.live_choice_channel)
    protected CustomFontTextView live_choice_channel;
    @BindView(R.id.live_choice_liu)
    protected CustomFontTextView live_choice_liu;

    @BindView(R.id.tv_1)
    protected com.ruihang.generalibrary.ui.widget.CustomFontTextView tv_1;
    @BindView(R.id.tv_2)
    protected com.ruihang.generalibrary.ui.widget.CustomFontTextView tv_2;
    @BindView(R.id.tv_3)
    protected com.ruihang.generalibrary.ui.widget.CustomFontTextView tv_3;
    @BindView(R.id.tv_4)
    protected com.ruihang.generalibrary.ui.widget.CustomFontTextView tv_4;


    private ArrayList<LiveAdvanceInfo> liveAdvanceInfos;
    private ChannelDiaFragment channelDiaFragment;

    protected BasePlayerView videoPlayer;

    private String tempName = "";
    public static final String CHANNEL_TAG = "dia_channel_fragment";
    private boolean heng = true;
    private boolean forward=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setThisImmersive(true);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_config);
        ButterKnife.bind(this);
        title_top_logo_layout.setVisibility(View.GONE);
        title_top_title.setVisibility(View.VISIBLE);
        title_top_title.setText("发起直播");
        title_top_title.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        getLiveConfig();

    }

    @OnClick({R.id.tv_start,R.id.title_top_back})
    public void onclick(View view) {
        switch (view.getId()) {
            case R.id.tv_start:

                if(!TextUtils.isEmpty(live_choice_liu.getText().toString())) {
                    ShareUtils.putString(this, "PushDomain", live_choice_liu.getText().toString());
                    ShareUtils.putBoolean(this, "heng", heng);
                    ShareUtils.putBoolean(this, "forward", forward);

                    startActivity(new Intent(this, CameraPusherActivity.class));
                }else{
                    toast("请选择推流地址");
                }

                break;
            case R.id.title_top_back:
                finish();
                break;
        }
    }



    /**
     * 请求直播配置数据
     */
    private void getLiveConfig() {
        if (!UserManager.isLoginS()) {
            return;
        }
        ParamsEditor pe = new ParamsEditor();
        pe.put("userId", UserManager.getInstance().getUser().getUserId());
        NetUtils.getNetAdapter().getLiveConfig(getOwnerName(), pe.getEncryptedParams(UserManager.getInstance().getToken())
                , new AbsNetCallBack<LiveAdvance>(LiveAdvance.class) {
                    @Override
                    public void onSuc(LiveAdvance liveAdvance) {
                        if (liveAdvance != null) {
                            JLog.e(liveAdvance.toString());
                            liveAdvanceInfos = liveAdvance.getList();
                        }
                    }
                });
    }






    @OnClick(value = {R.id.re_1,R.id.re_2,R.id.tv_1,R.id.tv_2,R.id.tv_3,R.id.tv_4})
    public void itemClick(View view) {
        switch (view.getId()) {
            case R.id.re_1:
            case R.id.re_2:
                if (!ListUtils.isListValued(liveAdvanceInfos)) {
                    Toast.makeText(this, "直播配置数据错误，请联系管理员！", Toast.LENGTH_SHORT).show();
                    return;
                }
                channelDiaFragment = new ChannelDiaFragment();

                channelDiaFragment.setOnSelectedListener(new ChannelDiaFragment.OnSelectedListener() {
                    @Override
                    public void onSelected(LiveAdvanceInfo info) {
                        JLog.e("Click:" + info.toString());
                        if (info != null) {
                            live_choice_channel.setText(info.getName());
                            tempName = info.getAddress();
                            live_choice_liu.setText(info.getAddress());
                        }
                        channelDiaFragment.dismissAllowingStateLoss();
                    }
                });
                channelDiaFragment.setDatas(liveAdvanceInfos);
                channelDiaFragment.setSelectedData(tempName);
                safeShowDialog(channelDiaFragment, CHANNEL_TAG);

                break;
            case R.id.tv_1:
                heng = true;

                Drawable drawable = getResources().getDrawable(R.mipmap.form_choice_select);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                tv_1.setDrawables(drawable, null, null, null);

                Drawable drawable2 = getResources().getDrawable(R.mipmap.form_choice_unchecked);
                drawable2.setBounds(0, 0, drawable2.getMinimumWidth(), drawable2.getMinimumHeight());
                tv_2.setDrawables(drawable2, null, null, null);




                break;
            case R.id.tv_2:
                heng = false;

                Drawable drawable3 = getResources().getDrawable(R.mipmap.form_choice_select);
                drawable3.setBounds(0, 0, drawable3.getMinimumWidth(), drawable3.getMinimumHeight());
                tv_2.setDrawables(drawable3, null, null, null);

                Drawable drawable4 = getResources().getDrawable(R.mipmap.form_choice_unchecked);
                drawable4.setBounds(0, 0, drawable4.getMinimumWidth(), drawable4.getMinimumHeight());
                tv_1.setDrawables(drawable4, null, null, null);




                break;
            case R.id.tv_3:
                forward = true;

                Drawable drawable5 = getResources().getDrawable(R.mipmap.form_choice_select);
                drawable5.setBounds(0, 0, drawable5.getMinimumWidth(), drawable5.getMinimumHeight());
                tv_3.setDrawables(drawable5, null, null, null);

                Drawable drawable6 = getResources().getDrawable(R.mipmap.form_choice_unchecked);
                drawable6.setBounds(0, 0, drawable6.getMinimumWidth(), drawable6.getMinimumHeight());
                tv_4.setDrawables(drawable6, null, null, null);




                break;
            case R.id.tv_4:
                forward = false;

                Drawable drawable7 = getResources().getDrawable(R.mipmap.form_choice_select);
                drawable7.setBounds(0, 0, drawable7.getMinimumWidth(), drawable7.getMinimumHeight());
                tv_4.setDrawables(drawable7, null, null, null);

                Drawable drawable8 = getResources().getDrawable(R.mipmap.form_choice_unchecked);
                drawable8.setBounds(0, 0, drawable8.getMinimumWidth(), drawable8.getMinimumHeight());
                tv_3.setDrawables(drawable8, null, null, null);


                break;
        }
    }




}
